package com.atguigu.gmall.item.service.impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.gmall.item.service.ItemService;
import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.product.BaseCategoryView;
import com.atguigu.gmall.model.product.SkuInfo;
import com.atguigu.gmall.model.product.SpuSaleAttr;
import com.atguigu.gmall.product.client.ProductFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author mqx
 * @date 2021-4-14 14:10:55
 */
@Service
public class ItemServiceImpl implements ItemService {

    //  远程调用service-product-client
    @Autowired
    private ProductFeignClient productFeignClient;
    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;
    @Autowired
    private ListFeignClient listFeignClient;

    @Override
    public Map<String, Object> getItemBySkuId(Long skuId) {
        //  声明对象
        Map<String, Object> result = new HashMap<>();

        try {
            CompletableFuture<SkuInfo> skuInfoCompletableFuture = CompletableFuture.supplyAsync(()->{
                //  获取到的数据是skuInfo + skuImageList
                SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
                result.put("skuInfo",skuInfo);
                return skuInfo;
            },threadPoolExecutor);

            CompletableFuture.allOf(skuInfoCompletableFuture,
                    skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
                        //  获取销售属性+销售属性值
                        List<SpuSaleAttr> spuSaleAttrListCheckBySku = productFeignClient.getSpuSaleAttrListCheckBySku(skuId, skuInfo.getSpuId());
                        result.put("spuSaleAttrList",spuSaleAttrListCheckBySku);
                    },threadPoolExecutor),

                    skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
                        //  查询销售属性值Id 与skuId 组合的map
                        Map skuValueIdsMap = productFeignClient.getSkuValueIdsMap(skuInfo.getSpuId());
                        //  将这个map 转换为页面需要的Json 对象
                        String valueJson = JSON.toJSONString(skuValueIdsMap);
                        result.put("valuesSkuJson",valueJson);
                    },threadPoolExecutor),

                    CompletableFuture.runAsync(()->{
                        //  获取价格
                        BigDecimal skuPrice = productFeignClient.getSkuPrice(skuId);
                        //  map 中 key 对应的谁? Thymeleaf 获取数据的时候 ${skuInfo.skuName}
                        result.put("price",skuPrice);
                    },threadPoolExecutor),

                    skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
                        //  获取分类数据
                        BaseCategoryView categoryView = productFeignClient.getCategoryView(skuInfo.getCategory3Id());
                        result.put("categoryView",categoryView);
                    },threadPoolExecutor),

                    //更新商品incrHotScore
                    CompletableFuture.runAsync(()->{
                        listFeignClient.incrHotScore(skuId);
                    },threadPoolExecutor)
                    ).join();
        } catch (Exception e) {
            e.printStackTrace();
            return result;
        }

        //  返回map 集合 Thymeleaf 渲染：能用map 存储数据！
        return result;
    }
}
